package com.panda.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.constants.SystemConstants;
import com.panda.entity.po.Category;
import com.panda.entity.vo.CategoryVo;
import com.panda.mapper.CategoryMapper;
import com.panda.service.ArticleService;
import com.panda.service.CategoryService;
import com.panda.utils.BeanCopyUtil;
import com.panda.utils.RespondResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类表(TCategory)表服务实现类
 *
 * @author makejava
 * @since 2022-03-15 15:34:01
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取所有分类
     * @return
     */
    @Override
    public RespondResult<List<CategoryVo>> getCategoryList() {
//        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
//        articleWrapper.eq(Article::getIsAudit, SystemConstants.ARTICLE_STATUS_NORMAL);
//        List<Article> articles = articleService.list(articleWrapper);
//        Set<Long> categoryIds = articles.stream()
//                .map(Article::getCategoryId)
//                .collect(Collectors.toSet());

        List<Category> categories = list();
        categories = categories.stream()
                //过滤通过审核的分类
                .filter(category -> SystemConstants.CATEGORY_STATUS_NORMAL.equals(category.getIsAudit()))
                .collect(Collectors.toList());
        //封装Vo
        List<CategoryVo> categoryVos = BeanCopyUtil.copyBeans(categories, CategoryVo.class);

        return RespondResult.okResult(categoryVos);
    }
}
